package servlet.manager;

import com.alibaba.fastjson.JSONObject;
import entity.Announcement;
import service.manager.AnnouncementService;
import service.manager.impl.AnnouncementServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * AnnouncementServlet
 *
 * @author 谭文斌
 * @since 2023/8/31
 */
@WebServlet("/manager/announcementServlet")
public class AnnouncementServlet extends HttpServlet {
    AnnouncementService service=new AnnouncementServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        PrintWriter out=resp.getWriter();

        // 准备返回参数
        int code = 0;
        String msg = "无数据";

        List<Announcement> list = service.getList();
        if (list==null){
            code=1;
        }

        //创建JSON对象
        JSONObject jsonResult = new JSONObject();

        jsonResult.put("code",code);
        jsonResult.put("msg",msg);
        jsonResult.put("data",list);

        out.print(jsonResult.toString());
    }
}
